//
//  MXHomePageTableView.m
//  MXUserSys
//
//  Created by 王启颖 on 2018/10/10.
//  Copyright © 2018年 王启颖. All rights reserved.
//

#import "MXHomePageTableView.h"
#import "MXHomePageCell.h"
#import "MXHomePageModel.h"

#import "MXDetialBeltController.h"
#import "MXCreateBeltController.h"

@interface MXHomePageTableView () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *paramsArr;
@property (nonatomic, assign) int currentPage;
@property (strong, nonatomic) NSMutableDictionary *dicParams;
@end

@implementation MXHomePageTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:self.bounds];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = MX_GRAY_COLOR;
        self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self addSubview:self.tableView];
        
        [self addHeaderRefresh];
        
    }
    return self;
}

#pragma mark - 上下拉刷新事件 -
- (void)addHeaderRefresh{
    //默认【下拉刷新】
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    //默认【上拉加载】
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}

-(void)refresh{
    self.currentPage = 1;
    
    [self.paramsArr removeAllObjects];
    [self getBeltRequest];
}

-(void)loadMore{
    self.currentPage ++;
    [self getBeltRequest];
}

#pragma mark - UITableViewDelegate -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.paramsArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MXHomePageCell *cell = [MXHomePageCell cellWithTableView:tableView];
    
    MXHomePageModel *model = _paramsArr[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85.f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MXHomePageModel *model = _paramsArr[indexPath.row];
    
    MXCreateBeltController *createBeltVC = [[MXCreateBeltController alloc]init];
    [self.navigationController pushViewController:createBeltVC animated:YES];
    createBeltVC.model = model;
 
    
}

#pragma mark - 懒加载 -
-(NSMutableArray *)paramsArr{
    if (!_paramsArr) {
        _paramsArr = [[NSMutableArray alloc]init];
    }
    return _paramsArr;
}

#pragma mark - 网络请求 -
-(void)isDelectByPidRequest:(NSString *)pid{
    [SVProgressHUD show];
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:pid,@"pid", nil];
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:isDelectByPid params:params isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];
        
        if (code == 0) {
            NSLog(@"%@",result);            
            [self getBeltRequest];
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"删除失败"];
        }
        
    } fail:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    
    
}

- (NSMutableDictionary *)dicParams {
    if (!_dicParams) {
        _dicParams = [NSMutableDictionary dictionary];
    }
    [self initdicParams];
    return _dicParams;
}
- (void)initdicParams {
    _dicParams[@"pageNum"] = [@(self.currentPage) description];
    _dicParams[@"pageSize"] = @"10";
    _dicParams[@"id"] = [MXUserMessageTool message].id;
    
}

-(void)getBeltRequest{
    [SVProgressHUD show];
    
    
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:getBelt params:self.dicParams isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if (code == 0) {
            NSLog(@"%@",result);
            
            
            
            for (id res in result[@"message"]) {
                MXHomePageModel *model = [MXHomePageModel mj_objectWithKeyValues:res];
                [self.paramsArr addObject:model];
            }
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"账号密码错误"];
        }
        
    } fail:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    
    
}


@end
